//
//  TYLanceSoldiersTemplate.m
//  AngerDragon
//
//  Created by gaotime_macbook_2 on 11-12-18.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TYLanceSoldiersTemplate.h"
#import "TYStateSystemProtocol.h"

#import "TYLifeSystemProtocol.h"
#import "TYGuerdonSystemProtocol.h"
#import "TYDisplaySystemProtocol.h"
#import "TYAttackSystemProtocol.h"

#import "TYPlayer.h"
#import "TYPlayerCommunicateProtocol.h"

 
@implementation TYLanceSoldiersTemplate

- (id) init {
	
	if ((self = [super init]))
	{
		CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
		
		/// 设置 长矛士兵第一个状态.
		[m_state_system_protocol setState:kLanceSoldiersState_debut];
	}
	
	return self;
}

- (void) dealloc {
	CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
	 
	
	[super dealloc];
}


#pragma mark 实现AI接口
- (void)ai:(ccTime) delta {
	
	NSInteger a_n_current_state = [m_state_system_protocol getCurrentlyState];
	switch (a_n_current_state) {
			
		/// 登场
		case kLanceSoldiersState_debut:
		{
			CCMoveTo* actionMove = [CCMoveTo actionWithDuration:10 
																								 position:[[TYPlayer getPlayerCommunicateProtocol] player_getPointOfCenter]];
			[self runAction:actionMove];
			
			/// 进入 "前进" 状态
			[m_state_system_protocol setState:kLanceSoldiersState_advance];
			/// 设置下一状态要显示的动画(采取这样提前更换下一状态的显示动画的方式来进行动画的更换)
			[m_display_system_protocol showAnimationForState:kLanceSoldiersState_advance];
		}break;
			
		/// 前进 
		case kLanceSoldiersState_advance:
		{
			if ([[TYPlayer getPlayerCommunicateProtocol] player_isCanAttackPlayer:self.position 
																														 attackDistance:[m_attack_system_protocol getAttackDistance]]) {
				[self stopAllActions];
				[m_display_system_protocol showAnimationForState:kLanceSoldiersState_attack];
				
				/// 进入有效的攻击范围, 更改状态
				[m_state_system_protocol setState:kLanceSoldiersState_attack];
				/// 设置下一状态要显示的动画(采取这样提前更换下一状态的显示动画的方式来进行动画的更换)
				[m_display_system_protocol showAnimationForState:kLanceSoldiersState_attack];
			}
		}break;
			
		/// 攻击
		case kLanceSoldiersState_attack:
		{
			[m_attack_system_protocol attackFromPoint:self.position.x 
																						atY:self.position.y 
																			timeDelta:delta];
		}break;

		/// 死亡
		case kSpriteState_Dead:
		{
			[self stopAllActions];
			[m_display_system_protocol showAnimationForState:kSpriteState_Dead];
		}break;

		default:
		{
		}break;
	}
}

@end
